const app = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        role: {
            type: "String",
            value: ""
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        searchValue: "",
        waitPeelList: [],
        qualityMap: {
            1: "精品",
            2: "次品",
            3: "统货",
        },
        isLastPage: false,
        current: 1,
        size: 10,
    },

    lifetimes: {
        attached: function () {
            // 在组件实例进入页面节点树时执行
            this.getData()
        },
        detached: function () {
            // 在组件实例被从页面节点树移除时执行
        },
    },

    pageLifetimes: {
        show: function () {
            this.onPullDownRefresh()
        },
    },

    /**
     * 组件的方法列表
     */
    methods: {
        onPullDownRefresh() {
            this.setData({
                current: 1,
                searchValue: "",
                waitPeelList: [],
                isLastPage: false
            })
            this.getData()
        },
        onReachBottom() {
            let {
                current,
                isLastPage
            } = this.data
            if (isLastPage) {
                return
            }
            this.setData({
                current: (Number(current) + 1)
            })
            this.getData()
        },
        onSearch() {
            this.setData({
                current: 1,
                waitPeelList: [],
                isLastPage: false
            })
            this.getData()
        },
        toDetail(e) {
            let item = e.currentTarget.dataset.item
            wx.navigateTo({
                url: `/pages/business/details/index?id=${item.id}`,
            })
        },
        syncToAccountBook(e) {
            let item = e.currentTarget.dataset.item
            item.orderType = 2
            if (this.data.role == 2) {
                item.type = 0
                item.financeName = "卖菜"
            } else {
                item.type = 1
                item.financeName = "买菜"
            }
            this.triggerEvent('bookPicker', item)
        },
        getData() {
            const {
                role,
                current,
                searchValue,
                size
            } = this.data;
            let api,
                params = {
                    current,
                    size,
                    cropName: searchValue,
                    enterStockStatus: 1
                }
            if (role == 2) {
                api = "getFarmerList"
            } else {
                api = "getMarketList"
                params.companyId = app.globalData.userInfo.companyData.companyId
            }
            wx.showLoading({
                title: "正在请求数据中"
            })
            wx.$apis[api](params).then(res => {
                wx.stopPullDownRefresh();
                let oldWaitPeelList = this.data.waitPeelList;
                let waitPeelList = res.data && res.data.records.map(item => {
                    item.createTimeStr = item.createTime.replace("T", " ")
                    return item
                })
                if (waitPeelList && waitPeelList.length) {
                    let newWaitPeelList = oldWaitPeelList.concat(waitPeelList)
                    this.setData({
                        waitPeelList: newWaitPeelList
                    })
                    if (waitPeelList.length < size) {
                        this.setData({
                            isLastPage: true
                        })
                    }
                } else {
                    this.setData({
                        isLastPage: true
                    })
                }
            }).catch(err => {
                wx.stopPullDownRefresh();
            })
        }
    }
})